#Binary parser template

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class HeroOnline_n3pmesh(Model3D):
    
    def __init__(self, obj3D=None, outFile=None, inFile=None):
        
        super(HeroOnline_n3pmesh, self).__init__("N3PMESH")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_file(self):
        
        charCount = self.inFile.read_long()
        meshName = self.inFile.read_string(charCount)
        self.create_object(meshName)
        
        unk1, unk2, vertNum, faceNum, unk3, unk4 = self.inFile.read_long(6)
        faceNum = faceNum / 3
        
        for i in range(vertNum):
            
            vx, vy, vz, nx, ny, nz, tu, tv = self.inFile.read_float(8)
            vx = float(vx) * -1.0
            self.create_vertex(meshName, i)
            self.add_coords(meshName, i, [vx, vy, vz])
            self.add_vert_normals(meshName, i, [nx, ny, nz])
            self.add_vert_uv(meshName, i, [tu, tv])
        
        #print "%d faces" %faceNum
        for i in range(faceNum):
            v1, v2, v3 = self.inFile.read_short(3)
            
            self.create_face(meshName, i)
            self.add_face_verts(meshName, i, [v1, v2, v3])
            self.add_face_material(meshName, i, 0)
            
        unk5, unk6, unk7, unk8, unk9 = self.inFile.read_long(5)
        if unk5 != 2:
            print "weird"
        
def read_file(path):
    
    openFile = StructReader(open(path, 'rb'))
    obj = HeroOnline_n3pmesh(inFile=openFile)
    obj.parse_file()
    openFile.close()
    return obj

def definitions():
    
    return "", "N3PMESH", "Hero n3pmesh"

if __name__ == '__main__':
    
    file1 = "bf_zone07_obj_11_02.n3pmesh"
    file2 = "obj_dun_pillar01_post.n3pmesh"
    read_file(file2)